﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace ERF.ContentProcessors
{
    [ContentProcessor]
    class BoxProcessor : ModelProcessor
    {
        private List<Vector3> vertices = new List<Vector3>();
        public override ModelContent Process(NodeContent input, ContentProcessorContext context)
        {
            FindVertices(input);


            //float maxX = float.MinValue;
            //float minX = float.MaxValue;
            //float maxZ = float.MinValue;
            //float minZ = float.MaxValue;
            //float maxY = float.MinValue;
            //float minY = float.MaxValue;
            //for (int i = 0; i < input.Children.Count; i++)
            //{
            //    if (input.Children[i] is MeshContent)
            //    {
            //        MeshContent meshContent = (MeshContent)input.Children[i];
            //        for (int j = 0; j < meshContent.Positions.Count; j++)
            //        {
            //            if (meshContent.Positions[i].X > maxX)
            //                maxX = meshContent.Positions[i].X;
            //            if (meshContent.Positions[i].X < minX)
            //                minX = meshContent.Positions[i].X;
            //            if (meshContent.Positions[i].Z > maxZ)
            //                maxZ = meshContent.Positions[i].Z;
            //            if (meshContent.Positions[i].Z < minZ)
            //                minZ = meshContent.Positions[i].Z;
            //            if (meshContent.Positions[i].Y > maxY)
            //                maxY = meshContent.Positions[i].Y;
            //            if (meshContent.Positions[i].Y < minY)
            //                minY = meshContent.Positions[i].Y;
            //        }
            //    }
            //}
            ModelContent model = base.Process(input, context);
            //model.Tag = new BoundingBox(new Vector3(minX, minY, minZ), new Vector3(maxX, maxY, maxZ));

            model.Tag = BoundingBox.CreateFromPoints(this.vertices);
            return model;
        }


        /// <summary>
        /// Helper for extracting a list of all the vertex positions in a model.
        /// </summary>
        void FindVertices(NodeContent node)
        {
            // Is this node a mesh?
            MeshContent mesh = node as MeshContent;
            if (mesh != null)
            {
                // Look up the absolute transform of the mesh.
                Matrix absoluteTransform = mesh.AbsoluteTransform;

                // Loop over all the pieces of geometry in the mesh.
                foreach (GeometryContent geometry in mesh.Geometry)
                {
                    // Loop over all the indices in this piece of geometry.
                    // Every group of three indices represents one triangle.
                    foreach (int index in geometry.Indices)
                    {
                        // Look up the position of this vertex.
                        Vector3 vertex = geometry.Vertices.Positions[index];

                        // Transform from local into world space.
                        vertex = Vector3.Transform(vertex, absoluteTransform);

                        // Store this vertex.
                        vertices.Add(vertex);
                    }
                }
            }

            // Recursively scan over the children of this node.
            foreach (NodeContent child in node.Children)
            {
                FindVertices(child);
            }
        }
    }
}
